﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using BTM_Client_Application.SupportClasses;
using Client_Server_Interface;

namespace BTM_Client_Application
{
    /// <summary>
    /// Логика взаимодействия для DeviceInfo.xaml
    /// </summary>
    public partial class DeviceInfoControl : IDisposable
    {
        public DeviceTreeItem.DeviceInfo DevInfo { get; private set; }

        private DeviceInfoControl()
        {
            InitializeComponent();
            UiLanguageChanged();
        }

        private DeviceInfoControl(DeviceTreeItem.DeviceInfo device) : this()
        {
            DevInfo = device;
            DeviceNameBox.Text = device.Title;
            StationBox.Content = device.ParentInfo.Station;
            DeviceBox.Content = device.Device;
            try
            {
                AdditionalInfoBox.Text = AliveInfo.DBwork.GetTreeItemTag(device.Title+device.ParentInfo.Station);
            }
            catch (Exception)
            {
            }
            
        }

        private delegate void ToExpose();

        private ToExpose _toExpose;

        private VoidDelegate _onShow;
        private VoidDelegate _onHide;

        public static DeviceInfoControl GetControl(IDeviceInterface device)
        {
            var thisControl = new DeviceInfoControl(device.DevInfo);
            thisControl.InitDataGrid();
            if (device.ErrorsList == null)
                device.ErrorsList = new ObservableCollection<ErrorListRecord>();
            thisControl.DataList.ItemsSource = device.ErrorsList;

            device.ErrorsListChanged += thisControl.DeviceOnErrorsListChanged;
            thisControl._toExpose = () =>
                {
                    device.ErrorsListChanged -= thisControl.DeviceOnErrorsListChanged;
                };

            thisControl._onShow += () => device.Opened = true;
            thisControl._onHide += () => device.Opened = false;
            return thisControl;
        }

        public static ToolTip GetOnToolTip(IDeviceInterface device)
        {
            var control = GetControl(device);
            var t = new ToolTip { Content = control };
            t.Style = ((DeviceInfoControl) t.Content).FindResource("MyToolTip1") as Style;
            t.Opened += (sender, args) => control.Show();
            t.Closed += (sender, args) => control.Hide();
            return t;
        }

        public static ToolTip GetOnToolTip(DeviceInfoControl device)
        {
            var control = device;
            var t = new ToolTip { Content = control };
            t.Style = ((DeviceInfoControl)t.Content).FindResource("MyToolTip1") as Style;
            t.Opened += (sender, args) => control.Show();
            t.Closed += (sender, args) => control.Hide();
            return t;
        }

        private void DeviceOnErrorsListChanged(object sender, EventArgs eventArgs)
        {
            Dispatcher.Invoke(new VoidDelegate(() =>
                {
                    DataList.ItemsSource = ((DeviceWorker.Device)sender).ErrorsList;
                }));
        }

        public void Show()
        {
            try
            {
                AdditionalInfoBox.Text = AliveInfo.DBwork.GetTreeItemTag(DeviceBox.Content + StationBox.Content.ToString());
            }
            catch (Exception)
            {
            }
            _onShow();
        }

        public void Hide()
        {
            _onHide();
        }

        private void InitDataGrid()
        {
            //var egc1 = new DataGridTextColumn
            //{
            //    //Header = Res.Rm.GetString("Region"),
            //    Header = "Region",
            //    Width = 150,
            //    Binding = new Binding("Region")
            //};
            //var egc2 = new DataGridTextColumn
            //{
            //    //Header = Res.Rm.GetString("Station"),
            //    Header = "Station",
            //    Width = 110,
            //    Binding = new Binding("Station")
            //};
            //var egc3 = new DataGridTextColumn
            //{
            //    //Header = Res.Rm.GetString("Device"),
            //    Header = "Device",
            //    Width = 140,
            //    Binding = new Binding("Device")
            //};
            var egc4 = new DataGridTextColumn
            {
                //Header = Res.Rm.GetString("Source"),
                Header = "Status",
                Width = 50,
                Binding = new Binding("Status")
            };
            var egc5 = new DataGridTextColumn
            {
                //Header = Res.Rm.GetString("Info"),
                Header = "Info",
                //Width = 230,
                Binding = new Binding("Info")
            };
            var egc6 = new DataGridTextColumn
            {
                //Header = Res.Rm.GetString("Date"),
                Header = "Date",
                Width = 140,
                Binding = new Binding("Date")
            };

            //DataList.Columns.Add(egc1);
            //DataList.Columns.Add(egc2);
            //DataList.Columns.Add(egc3);
            DataList.Columns.Add(egc4);
            DataList.Columns.Add(egc5);
            DataList.Columns.Add(egc6);
        }

        private void UiLanguageChanged() //Обновление языка интерфейса
        {
            StationLabel.Content = Res.Rm.GetString("Station", AliveInfo.CurrentCulture) + ":";
            DeviceLabel.Content = Res.Rm.GetString("DeviceType", AliveInfo.CurrentCulture) + ":";
            MahApps.Metro.Controls.TextboxHelper.SetWatermark(AdditionalInfoBox,
                                                              Res.Rm.GetString("AdditionalInfo",
                                                                               AliveInfo.CurrentCulture));
        }

        public void Dispose()
        {
            _toExpose();
        }

        private void AdditionalInfoBox_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            var t = sender as TextBox;
            try
            {
                AliveInfo.DBwork.SetTreeItemTag(DeviceNameBox.Text + StationBox.Content, t.Text);
            }
            catch (Exception ex)
            {
                t.IsEnabled = false;
                AliveInfo.ClientEvents.AddEvent(Res.Rm.GetString("DbError", AliveInfo.CurrentCulture), ex.Message);
            }
        }
    }
}
